#===============================================================================
#    RSSGlut id an opensource rss feed reader designed with python.
#    Copyright (C) 2008  Vijay Kartick
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#===============================================================================


from xml.sax.handler import ContentHandler
from xml.sax import make_parser
import sys,string

#===============================================================================
# global varialbes which contain the html code
# a contains the basic code,left is code for leftnavbar,
# main contains the parsed code
#===============================================================================

a=u"<html>"
left=u' '
main=u' '
curtitle=' '
curlink=' '
curdate=' '
curdesc=' '
curmisc=' '

#rssparser uses sax to parse xml.
class rssparser(ContentHandler):
    def __init__(self):

        self.check=0
        self.current=' '
        self.subcurrent=' '
    def startElement(self,name,attrs):
        name=string.lower(name)
        if ((self.check==0) and (name!='rss')):
            print >> sys.stderr, "Rss file not available"
            sys.exit(0)
        else:
            self.check=1
        #checks if file is of version 2.0
        if name=='rss' :
            if not (attrs.get('version')=='2.0') :
                print >> sys.stderr, "The file is not rss version 2.0"
                sys.exit(0)

        if name=='channel':
            self.current='channel'
        if name=='title':
            self.subcurrent='title'
        if name=='description':
            self.subcurrent='desc'
        if name=='link':
            self.subcurrent='link'
        if name=='pubdate':
            self.subcurrent='date'
        if name=='image' and self.current=='channel':
            self.current='chanimg'
        if name=='url':
            self.subcurrent='url'
        if name=='item':
            self.current="item"
        if name=='author':
            self.subcurrent='author'

#Creating Html page.This is getting complex.so a module for creating html page is needed.

    def characters(self,ch):
        global a,left,main,curtitle,curlink,curdesc,curmisc
        if (self.current=='channel'):
            if self.subcurrent=='title':
                a=a+'<link rel="stylesheet" type="text/css" \
                href="style.css" /><title>' +ch+ '</title><body><center><h2 id=top">'+ch+'</h2>'
                left=left+'<a href="#top">'+ch+'</a></center><br>'

            if self.subcurrent=='link':
                a=a+'link:<a href="'+ch+'">'+ch+'</a><br>'

            if self.subcurrent=='desc':
                a=a+'<divlass="desc"><strong>Description:</strong><br>'+ch+'</div><br>'

            if self.subcurrent=='date':
                a=a+'Date:'+ch+'<br>'

        if self.current=='chanimg':
            if self.subcurrent=='url':
                curlink=ch
            if self.subcurrent=='title':
                curtitle=ch
            if self.subcurrent=='desc':
                curdesc=ch

        if self.current=='item':
            if self.subcurrent=='title':
                left=left+'<a href="#'+ch +'">'+ch+'</a><br>'
                curtitle='<h3 id="'+ch+'">'+ch+'</h3>'
            if self.subcurrent=='desc':
                curdesc='<div class="desc"><strong>Description:</strong><br>'+ch+'<br></div>'
            if self.subcurrent=='link':
                curlink='link:<a href="'+ch+'">'+ch+'</a>'
            if self.subcurrent=='author':
                curmisc='From:<font color="brown">'+ch+'</font>'
            if self.subcurrent=='date':
                curdate='Date:'+ch


    def endElement(self,name):
        global a,left,main,curtitle,curlink,curdesc,curdate,curmisc
        name=string.lower(name)
        if name=='image':
            main=main+'<img src="'+curlink+'" alt="'+curtitle+'><br>'
        if name=='item':
            main=main+curtitle+'<br>'+curdesc+'<br>'+curdate+'<br>'+curlink+'<br>'+curmisc
            curtitle="Title not available"
            curdesc='description not available'
            curdate='date not available'
            curlink='link not available'
            curmisc='Unknown'
        self.subcurrent='nothing'


#write the parsed code to a html file
def writehtml():
        global a,left,main
        try:
            a=a+'<table frame="void" ><tr> \
            <td class="leftnav" width="175px" valign="top" >'+left+'</td><td class="main" >'+main+'</td></tr></table></body></html>'
            hdfile=open("rss.html","w")
            hdfile.write(a)
            hdfile.close()
            return True
        except:
            print "Unable to save html file"
            return False

def parse(file):
    parser=make_parser()
    xmlhandler=rssparser()
    parser.setContentHandler(xmlhandler)
    print "Parsing file"
    parser.parse(open(file))
    writehtml()



